//
// Created by sjjmw on 3/24/2022.
//

//应该使用dfs

#include "iostream"
#include "vector"
#include "list"
#include "algorithm"
#include "string"
#include "unordered_map"
#include "cmath"
#include<iomanip>
#include "string"
#include "map"
#include "list"

//从顶点开始扫描
struct Sol{
    std::vector<std::vector<int>> mat;
    std::vector<std::vector<char>> tag;
    int all_sum=0;
    int min_cnt=INT_MAX;
    int w;
    int h;
    void dfs(int x,int y,int cnt,int sum){
        if(x<0||x>=w||y<0||y>=h){
            return;
        }
        if(tag[y][x]==1){
            //走过
            return;
        }
        if(sum+mat[y][x]>all_sum/2){
            //超过一半
            return;
        }else if(sum+mat[y][x]==all_sum/2){
            //刚好一半
            min_cnt= std::min(min_cnt,cnt);
            return;
        }

        tag[y][x]=1;
        dfs(x+1,y,cnt+1,sum+mat[y][x]);
        dfs(x-1,y,cnt+1,sum+mat[y][x]);
        dfs(x,y+1,cnt+1,sum+mat[y][x]);
        dfs(x,y-1,cnt+1,sum+mat[y][x]);
        tag[y][x]=0;
    }
    void handle(
            int n,int m
            ){
        mat.reserve(n);
        tag.reserve(n);
        h=m;w=n;
        std::vector<int> matline(w);
        std::vector<char> tagline(w);
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                std::cin>>matline[j];
                all_sum+=matline[j];
            }
            mat.push_back(matline);
            tag.push_back(tagline);
        }
        dfs(0,0,1,0);
        printf("%d\n",min_cnt);
        ;
//        for (int i = 0; i < n; ++i) {
//            for (int j = 0; j < m; ++j) {
//
//            }
//        }
    }
};
int main(){
    int a,b,c;
//    float a,b,c,d;
    int e;
    while(std::cin>>a>>b){
        Sol().handle(a,b);

//        Sol().handle(a,b);
    }

    return 0;
}